package com.hxtec.polaris.controller;

import com.hxtec.polaris.commons.utils.IpUtil;
import com.hxtec.polaris.form.PayForm;
import com.hxtec.polaris.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author Marcus lv
 * @create 2019/12/6 4:15 下午
 */
@RestController
@RequestMapping(value = "/pay")
@Api(value = "/pay", tags = "支付")

public class PaymentController {
    private final PaymentService paymentService;

    public PaymentController(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    @GetMapping("/openid/{code}")
    public String openid(@PathVariable String code) throws IOException {
        return paymentService.openid(code);
    }

    @PostMapping("/prepare")
    @ApiOperation(value = "微信小程序支付")
    public Object mwx(@RequestBody PayForm form, HttpServletRequest request) throws Exception {
        form.setIp(IpUtil.getIpAddr(request));
        return paymentService.process(form);
    }
}
